package com.blindBox.back.controller.getDataController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.back.annotation.JwtToken;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.domain.Manager;
import com.blindBox.back.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//管理员管理
@RestController
public class BackDataManagerController
{
    @Autowired
    private ManagerService managerService;
    //获取所有管理员
    //@JwtToken
    @PostMapping("/get_all_managers")
    public MsgBackToFront get_all_managers()
    {
        List<Manager> managers = managerService.list();
        return managers==null?new MsgBackToFront(null,MsgBackToFront.NO_MANAGER)
                :new MsgBackToFront(managers,MsgBackToFront.HANDLE_SUCCESS);
    }

    //增加一个管理员
    @JwtToken
    @PostMapping("/add_one_manager")
    public MsgBackToFront add_one_manager(@RequestParam("m_name")String name,@RequestParam("m_pwd")String pwd)
    {
        Manager manager=new Manager();
        manager.setMName(name);
        manager.setMPwd(pwd);
        boolean save = managerService.save(manager);
        return save?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
    }

    //删除一个管理员
    @JwtToken
    @PostMapping("/del_one_manager")
    public MsgBackToFront del_one_manger(@RequestParam("m_id")Integer m_id)
    {
        boolean remove = managerService.removeById(m_id);
        return remove?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
    }

    //模糊搜索一个管理员
    @JwtToken
    @PostMapping("/get_target_manager")
    public MsgBackToFront find_one_manager(@RequestParam("m_name")String m_name)
    {
        List<Manager> managers = managerService.list(new QueryWrapper<Manager>().like("m_name", m_name));
        return new MsgBackToFront(managers,MsgBackToFront.HANDLE_SUCCESS);
    }
}
